﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ST.Common.Data;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Buz.Base;
using ST.Buz.Base.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using ST.Buz.Base.ViewModel;
using ST.Common.Util.Mvc;

namespace ST.Buz.Service
{
	public partial interface IServiceWXTplMsgLog
	{
		/// <summary>
		/// 分页列表
		/// </summary>
		PageData GetPageList(VMS_WX_TplMsgLog searchModel);

		/// <summary>
		/// 删除部门
		/// </summary>
		/// <param name="id"></param>
		/// <param name="sessionCurrent"></param>
		/// <returns></returns>
		bool Delete(int id, UserSession sessionCurrent);

		/// <summary>
		/// 获取查看时的详细信息
		/// </summary>
		/// <param name="id"></param>
		/// <param name="sessionCurrent"></param>
		/// <returns></returns>
		WX_TplMsgLog GetViewDetail(int id, UserSession sessionCurrent);
	}

	public partial class ServiceWXTplMsgLog
	{
		private readonly IServiceSysLog serviceSysLog;

		public ServiceWXTplMsgLog(BuzContext ctx,
			IServiceSysLog _serviceSysLog,
			ILogger<ServiceWXTplMsgLog> log)
		{
			serviceSysLog = _serviceSysLog;

			context = ctx;
			logger = log;
		}


		/// <summary>
		/// 获取查看时的详细信息
		/// </summary>
		/// <param name="id"></param>
		/// <param name="sessionCurrent"></param>
		/// <returns></returns>
		public WX_TplMsgLog GetViewDetail(int id, UserSession sessionCurrent)
		{
			try
			{
				var result = (from m in context.WXTplMsgLog
							  where m.ID == id
							  select m).AsNoTracking().SingleOrDefault();
				if (result == null)
				{
					throw new BuzException("参数有误");
				}
				return result;
			}
			catch (ArgumentException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "获取对象信息出错");
				throw new BuzException("获取对象信息失败，" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message));
			}
		}

		/// <summary>
		/// 分页列表
		/// </summary>
		public PageData GetPageList(VMS_WX_TplMsgLog searchModel)
		{
			PageData result = null;
			try
			{
				searchModel.Validate();

				var query = (from m in context.WXTplMsgLog
							 select m).AsNoTracking();
				//if (!string.IsNullOrEmpty(searchModel.name))
				//{
				//	query = query.Where(m => EF.Functions.Like(m.Name, "%" + searchModel.name + "%"));
				//}
				//if (!string.IsNullOrEmpty(searchModel.code))
				//{
				//	query = query.Where(m => m.Code == searchModel.code);
				//}
				//if (searchModel.ids != null && searchModel.ids.Length > 0)
				//{
				//	query = query.Where(m => searchModel.ids.Contains(m.ID));
				//}
				//if (searchModel.type.HasValue)
				//{
				//	query = query.Where(m => m.Type == searchModel.type.Value);
				//}
				//if (searchModel.orgid != null && searchModel.orgid.Length > 0)
				//{
				//	query = searchModel.orgid.Length == 1 ? query.Where(m => m.OrgID == searchModel.orgid[0]) : query.Where(m => searchModel.orgid.Contains(m.OrgID));
				//}
				switch (searchModel.sortby)
				{
					default:
						query = query.OrderByDescending(e => e.ID);
						break;
				}
				result = new PageData(searchModel.pnum.Value, searchModel.psize.Value);
				result.Load(query);
			}
			catch (BuzException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "获取分页数据出错");
				throw new BuzException("获取分页数据失败，" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message));
			}
			return result;
		}

		/// <summary>
		/// 删除参数
		/// </summary>
		/// <param name="id"></param>
		/// <param name="sessionCurrent"></param>
		/// <returns></returns>
		public bool Delete(int id, UserSession sessionCurrent)
		{
			bool success = false;
			try
			{
				//if (logger.IsEnabled(LogLevel.Debug))
				//{
				//	logger.LogDebug($"参数->id:{id}，state:{state}，remark:{remark}");
				//}
				if (sessionCurrent == null)
				{
					throw new BuzException("用户会话不能为空");
				}

				using (var trans = context.Database.BeginTransaction())
				{
					try
					{
						#region 事务具体操作
						DateTime dtNow = DateTime.Now;
						string ip = GetRequestIP();

						var modelOld = Get(id, true);
						Delete(modelOld);

						serviceSysLog.Add(sessionCurrent, ip, $"删除模板消息日志信息，原信息：", modelOld, dtNow);
						#endregion

						trans.Commit();
					}
					catch (Exception ex)
					{
						trans.Rollback();
						throw ex.InnerException ?? ex;
					}
				}

				success = true;
			}
			catch (BuzException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, "删除模板消息日志信息出错");
				throw new BuzException("删除模板消息日志信息失败，" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message));
			}
			return success;
		}

	}
}